﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_ListClass : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    DBClass dbobj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddepartment();
            bindgrid();
        }
    }
    protected void bindgrid()
    {
        string strsql = "";
        if (DropDownList1.SelectedIndex == 0)
            strsql = "select * from Class";
        else
            strsql = "select * from Class where DepartmentID='" + DropDownList1.SelectedValue.ToString().Trim() + "'";
        DataTable dt = dbobj.GetDataSet(strsql, "tabclass");
        GridView1.DataSource = dt.DefaultView;
        GridView1.DataKeyNames = new string[] { "ClassID" };
        GridView1.DataBind();       
        DropDownList ddl;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList2");
            DataRowView drv = dt.DefaultView[i];
            ddl.SelectedValue = drv["DepartmentID"].ToString();
        }
    }
    protected void binddepartment()
    { 
        string strsql = "select * from Department";
        DataTable dt = dbobj.GetDataSet(strsql, "tabdepartment");
        DropDownList1.DataSource = dt.DefaultView;
        DropDownList1.DataTextField = dt.Columns[1].ToString();
        DropDownList1.DataValueField = dt.Columns[0].ToString();
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "全部");
    }
    public DataTable getDepartment()
    {  
        string strsql = "select * from Department";
        DataTable dt = dbobj.GetDataSet(strsql, "tabdepartment");
        return dt;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        TextBox txtname = (TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("TextBox1");
        DropDownList ddl = (DropDownList)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownList2");
        Label lab = (Label)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("labClassID");
        if (e.CommandName == "upd")
        {
            string strsql = "update Class set ClassName='" + txtname.Text.Trim() + "',DepartmentID='" + ddl.SelectedValue.ToString() + "' where ClassID='" + lab.Text.Trim() + "'";
            dbobj.ExecNonQuery(strsql);
            bindgrid();
        }
        else if (e.CommandName == "del")
        {
            string strsql = "delete from Class where ClassID='" + lab.Text.Trim() + "'";
            dbobj.ExecNonQuery(strsql);
            bindgrid();
        }
        else
        {
            Session["Isdetail"] = 1;
            Response.Redirect("../Manager/ListTeacher.aspx?stdid=" + lab.Text.Trim());
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        DataTable dt = dbobj.GetDataSet("select * from Department", "tabdepartment");
        DropDownList3.DataSource = dt.DefaultView;
        DropDownList3.DataTextField = dt.Columns[1].ToString();
        DropDownList3.DataValueField = dt.Columns[0].ToString();
        DropDownList3.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindgrid();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text == "" || TextBox3.Text == "")
            Response.Write(ccobj.MessageBox("班级号和班级名称都不能为空"));
        else
        {
            string strsql = "insert into Class (ClassID,ClassName,DepartmentID) values('" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + DropDownList3.SelectedValue.ToString() + "')";
            dbobj.ExecNonQuery(strsql);
            bindgrid();
        }
        TextBox2.Text = "";
        TextBox3.Text = "";
        DropDownList3.SelectedIndex = 0;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
    }
}